class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        Boolean[] b = new Boolean[s.length()];
        return getIncludeStr(s, set, 0, b);
    }

    public boolean getIncludeStr(String s, Set<String> wordDict, int start, Boolean[] b) {
        if (start == s.length()) {
            return true;
        }
        if (b[start] != null) {
            return b[start];
        }

        for (int i = start + 1; i <= s.length(); i++) {
            if (wordDict.contains(s.substring(start, i)) && getIncludeStr(s, wordDict, i, b)) {
                b[start] = true;
                return true;
            }
        }
        b[start] = false;
        return false;

    }
}